//
//  AccountProvider.swift
//  RxStudy
//
//  Created by TRS-Mobile on 2024/6/7.
//  Copyright © 2024 season. All rights reserved.
//

import Foundation
import RxSwift

protocol UserAccountService {
    
    /// 登录
    /// - Parameters:
    ///   - name: 用户名
    ///   - pwd: 密码
    /// - Returns: 回调
    func login(name: String, pwd: String) -> Observable<Bool>
    
    /// 注册账号
    /// - Parameters:
    ///   - name: 用户名
    ///   - pwd: 密码
    ///   - repwd: 重新输入密码
    /// - Returns: 回调
    func register(name: String, pwd: String, repwd: String) -> Observable<Bool>
    
    /// 查询积分
    /// - Returns: 积分
    func queryUserCoin() -> Single<CoinRank>
    
    /// 查询未读消息数
    /// - Returns: 未读消息数
    func queryUserUnreadMessageCount() -> Single<Int>
}

class AccountProvider: UserAccountService {
    
    func register(name: String, pwd: String, repwd: String) -> RxSwift.Observable<Bool> {
        accountProvider.rx
            .request(.register(name, pwd, repwd))
            .map(BaseModel<AccountInfo>.self)
            .do { model in
                if model.isSuccess {
                    print("注册账号成功")
                }
            }
            .filter { $0.isSuccess }
            .asObservable()
            .flatMapLatest { [weak self] _ in
                self!.login(name: name, pwd: pwd)
            }
    }
    
    func queryUserCoin() -> RxSwift.Single<CoinRank> {
        myProvider.rx
            .request(.userCoinInfo)
            .map(BaseModel<CoinRank>.self)
            .compactMap { $0.data }
            .asObservable()
            .asSingle()
    }
    
    func queryUserUnreadMessageCount() -> RxSwift.Single<Int> {
        myProvider.rx
            .request(.unreadCount)
            .map(BaseModel<Int>.self)
            .compactMap { $0.data }
            .asObservable()
            .asSingle()
    }
    
    func login(name: String, pwd: String) -> RxSwift.Observable<Bool> {
        let result = accountProvider.rx
            .request(.login(name, pwd, true))
            .map(BaseModel<AccountInfo>.self)
            .do { model in
                if model.isSuccess {
                    AccountManager.shared
                        .saveLoginUsernameAndPassword(info: model.data, username: name, password: pwd)
                }
            }
            .map { model in
                model.isSuccess
            }
            .asObservable()
//            .asObservable()
//            .flatMapLatest { model in
//                if model.isSuccess {
//                    AccountManager.shared
//                        .saveLoginUsernameAndPassword(info: model.data, username: name, password: pwd)
//                }
//            }
//        accountProvider.rx
//            .request(AccountService.login(username, password, showLoading))
//            .retry(2)
//            .map(BaseModel<AccountInfo>.self)
//            .asObservable()
//            .flatMapLatest { (baseModel) -> Single<(CoinRank, Int)> in
//                if baseModel.isSuccess {
//                    AccountManager.shared.saveLoginUsernameAndPassword(info: baseModel.data, username: username, password: password)
//                }
//                return Single.zip(self.getMyCoin(), self.getMyUnreadMessageCount())
//            }
//            .asSingle()
//            .subscribe { event in
//                switch event {
//                case .success((let myCoin, let count)):
//     
//                    self.myCoinRelay.accept(myCoin)
//                    self.myUnreadMessageCountRelay.accept(count)
//                    
//                case .failure(_):
//                    self.myCoinRelay.accept(nil)
//                    self.myUnreadMessageCountRelay.accept(0)
//                }
//                completion?()
//            }.disposed(by: disposeBag)
        return result
    }
}
